fn main() {
    println!("Hello, world!");
    checked::op(1.0, 10.0)
}

mod checked {
    #[derive(Debug)]
    enum MathError {
        DivisionByZero,     //除0
        NegativeLogarithm,  //对数为负
        NegativeSquareRoot, //负平方根
    }

    type MathResult = Result<f64, MathError>;

    fn div(x: f64, y: f64) -> MathResult {
        if y == 0.0 {
            Err(MathError::DivisionByZero)
        } else {
            Ok(x / y)
        }
    }

    fn sqrt(x: f64) -> MathResult {
        if x < 0.0 {
            Err(MathError::NegativeSquareRoot)
        } else {
            Ok(x.sqrt())
        }
    }

    fn ln(x: f64) -> MathResult {
        if x < 0.0 {
            Err(MathError::NegativeLogarithm)
        } else {
            Ok(x.ln())
        }
    }

    fn op_(x: f64, y: f64) -> MathResult {
        let ratio = div(x, y)?;
        let ln = ln(ratio)?;
        sqrt(ln)
    }
    pub fn op(x: f64, y: f64) {
        match op_(x, y) {
            Ok(value) => println!("{}", value),
            Err(why) => panic!(
                "{}",
                match why {
                    MathError::DivisionByZero => "division by zero",
                    MathError::NegativeLogarithm => "logarithm of negative number",
                    MathError::NegativeSquareRoot => "square root of negative number",
                }
            ),
        }
    }
}
